package org.csu.mall.controller.front;

import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import com.google.gson.JsonObject;
import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Account;
import org.csu.mall.entity.CustomerOrder;
import org.csu.mall.service.ICustomerOrderService;
import org.csu.mall.service.IMerchantOrderService;
import org.csu.mall.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
//todo 待测
@Controller
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private ICustomerOrderService customerOrderService;

    @Autowired
    private IMerchantOrderService merchantOrderService;

    //生成顾客购物订单，但但订单状态为未支付，若订单选择货到付款，要等发货人确定才发货；若选择线上支付，则付款后发货
    @PostMapping("purchase_order")
    @ResponseBody
    public UniformResponse<CustomerOrderVO> purchaseOrder(CustomerOrder customerOrder, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        //测试
//        Account account1 = new Account();
//        account1.setUserId(1001);

        return customerOrderService.purchaseOrder(customerOrder, account);
    }

    //订单支付，若库存足够直接发货，库存不足，则等待发货。内部余额支付，未使用支付宝支付
    @PostMapping("order_payment")
    @ResponseBody
    public UniformResponse<String> orderPayment(Long orderId, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return customerOrderService.orderPayment(orderId, account);
    }

    @PostMapping("order_confirm")
    @ResponseBody
    public UniformResponse<String> orderConfirm(Long orderId, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return customerOrderService.orderConfirm(orderId, account);
    }

    @PostMapping("order_review")
    @ResponseBody
    public UniformResponse<String> orderReview(Long orderId, String comment, Integer star, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return customerOrderService.orderComment(orderId, comment, star, account);
    }

    @PostMapping("order_delivery")
    @ResponseBody
    public UniformResponse<String> orderDelivery(Long orderId, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return customerOrderService.orderDelivery(orderId, account);
    }

    @PostMapping("order_reject")
    @ResponseBody
    public UniformResponse<String> orderReject(Long orderId, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return customerOrderService.orderReject(orderId, account);
    }

    @PostMapping("customer_order_cancel")
    @ResponseBody
    public UniformResponse<String> customerOrderCancel(Long orderId, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return customerOrderService.customerOrderCancel(orderId, account);
    }

    @PostMapping("merchant_order_cancel")
    @ResponseBody
    public UniformResponse<String> merchantOrderCancel(Long orderId, HttpSession session){
        Account account = (Account)session.getAttribute((CONSTANT.LOGIN_USER));
        return merchantOrderService.merchantOrderCancel(orderId, account);
    }

    @GetMapping("customer_order_detail")
    @ResponseBody
    public UniformResponse<CustomerOrderVO> getCustomerOrderDetail(Long orderId, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        //测试
//        Account account1 = new Account();
//        account1.setUserId(1001);
        return customerOrderService.getCustomerOrderDetail(orderId, account);
    }

    @GetMapping("customer_order_list")
    @ResponseBody
    public UniformResponse<CustomerOrderListVO> getCustomerOrderList(
            @RequestParam(name = "orderId", required = false) Long orderId,
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(name = "orderSource", defaultValue = "2") Integer orderSource,
            @RequestParam(name = "orderState", defaultValue = "-1") Integer orderState,
            @RequestParam(name = "deliveryStatus", defaultValue = "-1") Integer deliverStatus,
            HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        //测试
//        Account account1 = new Account();
//        account1.setUserId(1001);
//        account1.setRole(0);
        return customerOrderService.getCustomerOrderList(orderId, keyword, pageSize, pageNum, orderSource, orderState, deliverStatus, account);
    }

    @GetMapping("merchant_order_detail")
    @ResponseBody
    public UniformResponse<MerchantOrderVO> getMerchantOrderDet1il(Long orderId, HttpSession session){
                Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        //测试
//        Account account1 = new Account();
//        account1.setUserId(1001);
//        account1.setRole(0);
        return merchantOrderService.getMerchantOrderDetail(orderId,account);
    }

    @GetMapping("merchant_order_list")
    @ResponseBody
    public UniformResponse<MerchantOrderListVO> getMerchantOrderList(
            @RequestParam(name = "orderId", required = false) Long orderId,
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(name = "orderSource", defaultValue = "2") Integer orderSource,
            @RequestParam(name = "orderState", defaultValue = "-1") Integer orderState,
            HttpSession session){
//        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        //测试
        Account account1 = new Account();
        account1.setUserId(1001);
        account1.setRole(0);
        return merchantOrderService.getMerchantOrderList(orderId, keyword, pageSize, pageNum, orderSource, orderState, account1);
    }

    @PostMapping("alipay_trade_precreate")
    @ResponseBody
    public UniformResponse<AlipayVO> tradePrecreate(Long orderId, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
//        Account account1 = new Account();
//        account1.setUserId(1001);
        return customerOrderService.tradePrecreate(orderId, account);
    }

}
